import { tenantHttp, loginHttp } from '/@/utils/http/axios';
import {
  LoginParams,
  LoginResultModel,
  GetUserInfoModel,
  JumpLoginParams,
  JumpLoginResultModel,
  SaaSRegisterParams,
  SaaSLogOutParams,
  SaaSLoginParams,
  getAllAppParams,
  LabelListItem,
} from './model/userModel';
import { Record } from '/@/../mock/login/accessApp';
import { Data } from '/@/../mock/login/login';

import { ErrorMessageMode } from '/#/axios';
import { ChooseAppParams, SlLogoutParams } from './model/userModel';

// 203
// Bug fix网关地址：http://100.85.127.149:9702
// Master网关地址：http://100.93.0.245:9702
// oneaccess：https://ffhxiukmkf.huaweicloudoneaccess.com/user

// 202
// 网关地址：http://100.85.255.44:9702
// oneaccess: https://hkkgrnnagd.huaweicloudoneaccess.com/user

export const TENANT_PATH = 'http://100.93.0.245:9702/saaslandingTenant';
export const ONEACCESS_LOGIN_PATH = 'https://ffhxiukmkf.huaweicloudoneaccess.com/user';

export const LOGIN_URL = '/origin/login';
export const LOGOUT_URL = '/origin/logout';

enum Api {
  Login = '/v1/saaslanding/login',
  Register = '/origin/register',
  add = '/origin/addTenantAccount',
  Logout = '/origin/logout',
  OriginLogin = '/login',
  GetUserInfo = '/getUserInfo',
  GetPermCode = '/getPermCode',
  GetUserRole = '/getUserRole',
  GetAllApp = '/v1/saaslanding/getAppListByUserId',
  ChooseApp = '/v1/saaslanding/chooseApp',
  SaaslandingLogout = '/v1/saaslanding/saaslandingLogout',
  OauthLogout = '/v1/saaslanding/oneaccessLogout',
  CheckLoginStatus = '/v1/saaslanding/checkLoginStatus',
  GET_LABEL_LIST = '/origin/label/listLabelMap',
  GET_CONSOLE_URL = '/v1/saaslanding/loginConsole',
}

/**
 * @description: SaaS Landing login api
 */
export function loginSaaSApi(params: SaaSLoginParams) {
  return loginHttp.post<Data>({
    url: Api.Login,
    params,
  });
}

export function getAllApp(params: getAllAppParams) {
  return loginHttp.post<{
    total: number;
    items: Record[];
  }>({
    url: Api.GetAllApp,
    params,
  });
}

export function chooseApp(params: ChooseAppParams) {
  return loginHttp.post<string>({
    url: Api.ChooseApp,
    params,
  });
}

export function oauthLogout(params: { sessionId: string }) {
  return loginHttp.get<{
    location: string;
    path: string;
  }>({
    url: Api.OauthLogout,
    params,
  });
}

export function saasLandingLogout(params: SlLogoutParams) {
  return loginHttp.get<void>({
    url: Api.SaaslandingLogout,
    params,
  });
}
export function checkLoginStatus(params: SlLogoutParams) {
  return loginHttp.get<{
    path: string;
    location: string;
  }>({
    url: Api.CheckLoginStatus,
    params,
  });
}

/**
 * @description: SaaS Landing logout api
 */
export function logoutSaaSApi() {
  return loginHttp.get<null>({ url: Api.Logout });
}

/**
 * @description: SaaS Landing get login user role api
 */
export function getUserRole(params: JumpLoginParams) {
  return tenantHttp.get<JumpLoginResultModel>({ url: Api.GetUserRole, params });
}

export const getLabelList = () => {
  return tenantHttp.get<LabelListItem>({ url: Api.GET_LABEL_LIST });
};

/**
 * @description: SaaS Landing user register api
 */
export function registerSaaSUser(params: SaaSRegisterParams) {
  // 注册提交参数
  return loginHttp.post<string>({ url: Api.Register, params });
}

export function addTenantAccount(params: SaaSRegisterParams) {
  // 注册提交参数
  params.type = 1;
  return loginHttp.post<string>({ url: Api.add, params });
}
/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return tenantHttp.post<LoginResultModel>(
    {
      url: Api.OriginLogin,
      params,
    },
    {
      errorMessageMode: mode,
    }
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return tenantHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
}

export function getPermCode() {
  return tenantHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout(params: SaaSLogOutParams) {
  return tenantHttp.get({
    url: Api.Logout + '/' + params.env.toString() + '/' + params.app.toString(),
  });
}
